import SVGA from 'svgaplayerweb'
import getfile from './getfile.js'
export default {
	data() {
		return {
			player:null,
			parser:null,
			pdata:{}
		}
	},
	methods: {
		async render(val,oldValue,vm) {
			let data,player,parser;
			if(val){
				data =JSON.parse(val)
				this.pdata=data
			}else{
				player=this.player
				parser=this.parser
				data=this.pdata
			}
			if(!data.src){
				console.error('缺少src');
				return 
			} 
			if(!this.player){
				player = new SVGA.Player('#'+data.myCanvasId);
				parser = new SVGA.Parser();
			}
			player.loops=data.loops
			player.clearsAfterStop=data.clearsAfterStop
			player.fillMode=data.fillMode
			parser.load(await getfile(data.src),(videoItem)=>{
				player.setVideoItem(videoItem);
				if(data.autoPlay){
					player.startAnimation();
				}
			})
			player.onFinished(()=>{ //只有在loop不为0时候触发
				// console.log('动画停止播放时回调');
				vm.callMethod('receiveRenderData',{name:'onFinished'})
			})
			if(data.isOnChange){
				player.onFrame(frame=>{ //动画播放至某帧后回调
					// console.log(frame);
					vm.callMethod('receiveRenderData',{name:'onFrame',val:frame})
				})
				player.onPercentage(percentage=>{ //动画播放至某进度后回调
					// console.log(percentage);
					vm.callMethod('receiveRenderData',{name:'onPercentage',val:percentage})
				})
			}
			
			this.player=player
			this.parser=parser
		},
		async callPlayer(val){
			if(!val.name)return;
			let {name, args} = val
			console.log(name, args);
			if(name=='setImage'){
				args[0]=await getfile(args[0])
			}
			if(name=='clearDynamicObjects'){
				const parser = new SVGA.Parser();
				parser.load(await getfile(this.pdata.src),(videoItem)=>{
					this.player.setVideoItem(videoItem);
					this.player.startAnimation();
				})
			}
			if(Array.isArray(args)){
				this.player[name](...args)
			}else{
				this.player[name](args)
			}
		}
		
	},
	mounted() {
	}
}